<?php require_once('../Connections/cotiza.php'); ?>

<?php

	$query	= "SELECT DISTINCT 
    solic_cliente,
    solic_sku, 
    concat(solic_serie, solic_version) as cod_articulo, 
    solic_npcliente 
FROM 
    solic_mstr
WHERE
    solic_serie <> ''   AND
    solic_version <> '' AND
    solic_sku <> ''
ORDER BY 
    solic_cliente
";

	$fp = fopen("/u1/apache/htdocs/cotizador/prod/data/sku.csv", "w");
	$rsl   = mysql_query($query,$cotiza);
	
	fwrite($fp, "cliente;sku;articulo;descripcion;\n");
	
	while ($row   = mysql_fetch_assoc($rsl))
	{
		$aux	= explode("-", $row['solic_cliente']);
		fwrite($fp, $aux[0].";".$row['solic_sku'].";".$row['cod_articulo'].";".$row['solic_npcliente'].";\n");
	}
	fclose($fp);
	
	
	
	
		$query	= "SELECT 
	solic_id,
    concat(solic_serie, solic_version) AS cod_articulo, 
    solic_cantidad          AS cantidad,
    solic_um                AS um,
    result_peso_prod_br     AS peso_prod_bruto,
    result_peso_prod        AS peso_prod_neto,
    result_peso_pedido      AS peso_pedido, 
    result_costo            AS costo, 
    result_margen           AS margen, 
    result_precio           AS precio, 
    result_moneda           AS moneda,
    ( result_peso_prod_br * solic_cantidad / 1000 )    AS peso_total_bruto_kg,
    ( result_costo * solic_cantidad )           AS costo_total,
    ( result_precio * solic_cantidad )          AS precio_total,
    ( ( result_costo * solic_cantidad ) / ( result_peso_prod_br * solic_cantidad / 1000 ) )        AS costo_por_kilo,
    ( ( result_precio * solic_cantidad ) / ( result_peso_prod_br * solic_cantidad / 1000 ) )       AS precio_por_kilo

FROM 
    solic_mstr, result_mstr
    
WHERE
    solic_mstr.solic_id = result_mstr.result_solic_id   AND
    solic_mstr.solic_status = 5                         AND
    solic_mstr.solic_um = 'UN'                          AND
    result_mstr.result_peso_prod_br <> ''
    
ORDER BY 
    solic_serie,
    solic_version,
    solic_id
";

	$fp = fopen("/u1/apache/htdocs/cotizador/prod/data/costo_y_precio.csv", "w");
	$rsl   = mysql_query($query,$cotiza);
	
	fwrite($fp, "solic_id;cod_articulo;cantidad;um;peso_prod_bruto;peso_prod_neto;peso_pedido;costo;margen;precio;moneda;peso_total_bruto_kg;costo_total;precio_total;costo_por_kilo;precio_por_kilo\n");
	
	while ($row   = mysql_fetch_assoc($rsl))
	{
		fwrite($fp, $row['solic_id'].";".$row['cod_articulo'].";".$row['cantidad'].";".$row['um'].";".$row['peso_prod_bruto'].";".$row['peso_prod_neto'].";".$row['peso_pedido'].";".$row['costo'].";".$row['margen'].";".$row['precio'].";".$row['moneda'].";".$row['peso_total_bruto_kg'].";".$row['costo_total'].";".$row['precio_total'].";".$row['costo_por_kilo'].";".$row['precio_por_kilo'].";\n");
	}
	fclose($fp);
	
	
?>


